//
//  BusLineDetailViewController.m
//  Bus_iPhone
//
//  Created by antunnel on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BusLineDetailViewController.h"
#import "BusDataManager.h"

@interface BusLineDetailViewController ()

@end

@implementation BusLineDetailViewController

@synthesize segment=_segment;
@synthesize busLineInfo=_busLineInfo;
@synthesize busStationInfo=_busStationInfo;


-(void)dealloc{
    
    [_segment release];
    [_busLineInfo release];
    [_busStationInfo release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setCustomNavigationReturnItem];
    
    // Do any additional setup after loading the view from its nib.
}


-(IBAction)segmentChange:(id)sender{
    
    [self.tableView reloadData];
}


#pragma mark -UITableViewDataSource

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return [_busLineInfo objectForKey:@"lineInfo"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_segment.selectedSegmentIndex==0) {
        return [[_busStationInfo objectForKey:@"go"]count];
    }else if (_segment.selectedSegmentIndex==1) {
        return [[_busStationInfo objectForKey:@"back"]count];
    }else {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellString=@"bus line state ";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellString];
    if(!cell){
        
        cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellString] autorelease];
        cell.backgroundColor=[UIColor clearColor];
    }
    NSArray *arrray=nil;
    if (_segment.selectedSegmentIndex==0) {
        arrray=[_busStationInfo objectForKey:@"go"];
    }else if (_segment.selectedSegmentIndex==1) {
        arrray=[_busStationInfo objectForKey:@"back"];
    }else {
        return 0;
    }
    NSDictionary *dic=[arrray objectAtIndex:indexPath.row];
    cell.textLabel.text=[NSString stringWithFormat:@"%d %@",[[dic objectForKey:@"theIndex"] intValue]+1,[dic objectForKey:@"stopName"]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSArray *arrray=nil;
    if (_segment.selectedSegmentIndex==0) {
        arrray=[_busStationInfo objectForKey:@"go"];
    }else if (_segment.selectedSegmentIndex==1) {
        arrray=[_busStationInfo objectForKey:@"back"];
    }
    NSDictionary *dic=[arrray objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ShowBusStopDetailNotication" object:dic];
}


@end
